package org.jivesoftware.spark.crowd.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.spark.crowd.CrowdsManager;
import org.jivesoftware.spark.crowd.Param;
import org.jivesoftware.spark.crowd.packet.CrowdsMessageManager;
import org.jivesoftware.spark.crowd.packet.ReceivedPacketProcesser;
import org.jivesoftware.spark.crowd.ui.UIEventManager;
import org.jivesoftware.spark.util.ModelUtil;

public class Crowd{
	
	
	
	private String crowdJID;
	
	private String name;
	private Member creator;
	private String createTime;
	private String title;
	
	private boolean membersLoaded;
	private ReceivedPacketProcesser packetProcesser;
	/**
	 * KEY : bare JID .i consider that all user use the same client and the same version.
	 */
	private Map<String,Member> members = new HashMap<String,Member>();

	public Crowd(String name,Member creator,String crowdJID,String createTime,String title){
		
		this.name = name;
		
		this.creator = creator;
		
		this.crowdJID = crowdJID;
		this.createTime = createTime;
		this.title = title;
		
		this.packetProcesser = ReceivedPacketProcesser.getInstance();
		
	}
	public Crowd(String name,Member creator,String title){	
		this(name,
			creator,
			name + "@" + Param.getFullCrowdDomain(),
			Param.parseTime(System.currentTimeMillis()),
			title);		
	}
	

	public void setName(String name) {
		this.name = name;
	}

	public List<Member> getMembers() {
		List<Member> ms = new ArrayList<Member>();
		for(Member m : members.values()){
			ms.add(m);
		}
		ms.add(creator);
		return ms;
	}

	

	public Member getCreator() {
		return creator;
	}
	

	

	public String getCrowdJID() {
		return crowdJID;
	}
	
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setMembers(Map<String, Member> members) {
		this.members = members;
	}
	
	public void addOrUpdateMember(Member m) {
			members.put(m.getJID(), m);		
	}

	
	public Message sendMessage(String text) throws XMPPException {

        text = text.replaceAll("[\\u0001-\\u0008\\u000B-\\u001F]", "");

        if (!ModelUtil.hasLength(text)) {
            return null;
        }

        CrowdsManager.getInstance().filterOutgoingMessage(text);
  
		return CrowdsMessageManager.getInstance(Param.getConnection(),Param.CROWD_SERVICE_NAME).sendMessage(name,text);			
	}

	public void showMemberInfo(String memberName) {
		// TODO Auto-generated method stub
		
	}

	
	public void receiveMessage(Message message) {
		// TODO Auto-generated method stub
		
	}
	
	public String getName() {
		
		return this.name;
	}
	
	public Member findMemberByJID(String bareJID){	
		Member m = members.get(bareJID);
		
		if(m == null && creator.getJID().equals(bareJID))
			m = creator;		
		return m;	
	}
	
	/**
	 * when receive members from CrowdMembersIQProvider,add them into members list.
	 */
	public void receiveMembersInACrowd(List<Member> members) {
		for(Member m : members){
			addOrUpdateMember(m); 
		}
	}
	/**
	 * 
	 * @param nickName
	 * @return
	 */
	public Member FindMemberByNickName(String nickName){
		if(creator.getNickName().equals(nickName))
			return creator;
		Collection<Member> ms = members.values();
		for(Member m : ms){
			if(m.getNickName().equals(nickName))
				return m;
		}
		return null;
	}
	
	public void removeMember(Member rm){
		members.remove(rm.getJID());
	}
	public void removeMember(String rmJID){
		members.remove(rmJID);
	}
	public void removeAllMembers(){
		members.clear();
		setMembersLoaded(false);
	}
	public boolean isMembersLoaded() {
		return membersLoaded;
	}
	public void setMembersLoaded(boolean membersLoaded) {		
			this.membersLoaded = membersLoaded;
			synchronized(UIEventManager.getInstance()){
				UIEventManager.getInstance().notifyAll();
			}
	}
	
	
	

}
